package cn.sdormitory.canteen.service.impl;


import cn.sdormitory.canteen.dao.FoodSampleDao;
import cn.sdormitory.canteen.service.FoodSampleService;
import cn.sdormitory.canteen.vo.FoodSampleVO;
import cn.sdormitory.common.utils.SysUserUtils;
//import cn.sdormitory.mq.RabbitSenderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class FoodSampleServiceImpl extends ServiceImpl<FoodSampleDao, FoodSampleVO> implements FoodSampleService {

    @Autowired
    private FoodSampleDao foodSampleDao;

//    @Autowired
//    private RabbitSenderService rabbitSender;


    @Override
    public IPage<FoodSampleVO> getPage(Map<String, Object> params) {

        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        Object sampleName = params.get("sampleName");
        Map<String,Object> map = null;

        LambdaQueryWrapper<FoodSampleVO> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(sampleName)) {
            wrapper.likeRight(FoodSampleVO::getSampleName, sampleName);
        }
        wrapper.orderByDesc(FoodSampleVO::getCreatedTime);
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public int saveOrUpdateFoodSample(FoodSampleVO vo) {
        int num=0;
        String currBy = SysUserUtils.getSysUser().getUsername();
        Date currDate = new Date();
        if(StringUtils.isEmpty(vo.getId())){
            vo.setCreateBy(currBy);
            vo.setCreatedTime(currDate);
            num = foodSampleDao.insert(vo);
        }else{
            vo.setModifyBy(currBy);
            vo.setModifyTime(currDate);
            num = foodSampleDao.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteFoodSampleById(Long id) {
        return foodSampleDao.deleteById(id);
    }
}
